package com.redislock.aop.tool.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.redislock.aop.tool.bean.RedisLockDefinitionHolder;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;


import javax.annotation.PostConstruct;
import java.util.Iterator;
import java.util.concurrent.*;

@Log4j2
@Component
public class ScheduleUtils {
    public static ConcurrentLinkedQueue<RedisLockDefinitionHolder> redisLockDefinitionHolders = new ConcurrentLinkedQueue<>();

    @PostConstruct
    public void init() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
        //每10秒实现检查key是否过期，如果超过key的生命周期的三分之一的话，那么就需要为这个key续时下
        scheduledExecutorService.scheduleAtFixedRate(
                () -> {
                    if (CollUtil.isNotEmpty(redisLockDefinitionHolders)) {
                        Iterator<RedisLockDefinitionHolder> iterator = redisLockDefinitionHolders.iterator();
                        while (iterator.hasNext()) {
                            RedisLockDefinitionHolder redisLockDefinitionHolder = iterator.next();
                            if (BeanUtil.isEmpty(redisLockDefinitionHolder)) {
                                iterator.remove();
                                continue;
                            }

                            //一当key无效的话，那么就删除
                            if (ObjectUtil.isEmpty(RedisUtil.get(redisLockDefinitionHolder.getBusinessKey()))) {
                                log.error("key无效了！");
                                iterator.remove();
                                continue;
                            }

                            if (redisLockDefinitionHolder.getCurrentCount() > redisLockDefinitionHolder.getTryCount()) {
                                log.error("线程中断");
                                redisLockDefinitionHolder.getCurrentThread().interrupt();
                                iterator.remove();
                                continue;
                            }

                            Long currentTime = System.currentTimeMillis();
                            //一当当前的时间超过上一次时间+间隔时间的话，说明马上超时了
                            if ((redisLockDefinitionHolder.getLastModifyTime() + redisLockDefinitionHolder.getModifyPeriod()) < currentTime) {
                                log.error("超过3分之一了，继续续时");
                                try {
                                    redisLockDefinitionHolder.setModifyPeriod(currentTime);
                                    redisLockDefinitionHolder.setCurrentCount(redisLockDefinitionHolder.getCurrentCount() + 1);
                                    RedisUtil.expire(redisLockDefinitionHolder.getBusinessKey(), redisLockDefinitionHolder.getLockTime(), TimeUnit.SECONDS);
                                } catch (Exception e) {
                                    log.info(e.getMessage());
                                }
                                continue;
                            }
                        }
                    }
                }, 0, 10, TimeUnit.SECONDS);
    }
}
